package com.gxz.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@Controller
public class RequestController {
    @GetMapping("/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("user", "小郭"); //向request域中添加了数据
        request.getSession().setAttribute("website","http://www.baidu.com");
        return "forward:/ok"; //转发到/ ok
    }

    @ResponseBody
    @GetMapping("/ok")
    public String ok(@RequestAttribute(value = "user",required = false) String username, HttpServletRequest request , @SessionAttribute(value = "website",required = false) String website) {

        //获取request域中的数据
        System.out.println("username=" +username);
        System.out.println("通过servlet api 获取 username-" +request.getAttribute("user"));
        System.out.println("website-"+website);
        System.out.println("通过servlet api 获取 website-" +request.getSession().getAttribute("website"));
        return "success";
    }

    //响应一个注册请求
    @GetMapping("/register")
    public String register(Map<String,Object> map, Model model, HttpServletResponse response){
        //如果注册一个请求,会将注册数据封装到map或者model
        //map中的数据和model的数据 ,会被放入到request中
        map.put("user","gxz");
        map.put("job","架构师");
        model.addAttribute("sal",80000);
        Cookie cookie = new Cookie("email","80000");
        response.addCookie(cookie);
        //请求转发
        return "forward:/registerOk";

    }

    @ResponseBody
    @GetMapping("/registerOk")
    public String registerOK(HttpServletRequest request){
        System.out.println("user-"+request.getAttribute("user"));
        System.out.println("job-"+request.getAttribute("job"));
        System.out.println("sal-"+request.getAttribute("sal"));
        return "success";
    }




}
